﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainProject.Quanlychiphixangdau
{
    public partial class Suachiphixangdau : Form
    {
        int idchiphi;
        public Suachiphixangdau()
        {
            InitializeComponent();
        }
        public Suachiphixangdau(int id)
        {
            InitializeComponent();
            idchiphi = id;
            loaddata();
        }

        private void loaddata()
        {
            QLXT_DBDataContext db = new QLXT_DBDataContext();
            CHIPHIXANGDAU tt = db.CHIPHIXANGDAUs.Single(p => p.IDCHIPHIXANGDAU == idchiphi);

            cmb_Idxe.DataSource = from p in db.XEs select p;
            cmb_Idxe.DisplayMember = "BIENSO";
            cmb_Idxe.ValueMember = "IDXE";
            cmb_Idxe.SelectedValue = tt.IDXE;

            cmb_Tuyendo.DataSource = from p in db.TUYENs select p;
            cmb_Tuyendo.ValueMember = "IDTUYEN";
            cmb_Tuyendo.DisplayMember = "TENTUYEN";
            cmb_Tuyendo.SelectedValue = tt.TUYEN;

            cmb_Nguoido.DataSource = from p in db.TAIXEs select p;
            cmb_Nguoido.ValueMember = "IDTAIXE";
            cmb_Nguoido.DisplayMember = "TEN";
            cmb_Nguoido.SelectedIndex = tt.NGUOIDO;

            txt_Solit.Text = (tt.SOLIT).ToString();
            txt_Dongia.Text = (tt.DONGIA).ToString();
            txt_Cayxang.Text = tt.CAYXANG;
            txt_Hoadonbanle.Text = tt.HOADONBANLE;

            date_Ngaydo.Value = (DateTime)tt.NGAYDO;
            rtxt_Ghichu.Text = tt.GHICHU;
            txt_ID.Text = (tt.IDCHIPHIXANGDAU).ToString();
        }

        private void bnt_Sua_Click(object sender, EventArgs e)
        {
            if (txt_Solit.Text == string.Empty || txt_Dongia.Text == string.Empty)
            {
                MessageBox.Show(@"Vui lòng nhập đủ thông tin về số lít và đơn giá", @"Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            QLXT_DBDataContext qlxt_db = new QLXT_DBDataContext();

            try
            {
                CHIPHIXANGDAU tt = qlxt_db.CHIPHIXANGDAUs.Single(p => p.IDCHIPHIXANGDAU == idchiphi);

                tt.DONGIA = double.Parse(txt_Dongia.Text);
                tt.SOLIT = double.Parse(txt_Solit.Text);
                tt.GHICHU = rtxt_Ghichu.Text;
                tt.TONGTIEN = double.Parse(txt_Tongtien.Text);
                tt.TUYEN = (int)cmb_Tuyendo.SelectedValue;
                tt.IDXE = (int)cmb_Idxe.SelectedValue;
                tt.NGAYDO = date_Ngaydo.Value;
                tt.NGUOIDO = (int)cmb_Nguoido.SelectedValue;
                tt.CAYXANG = txt_Cayxang.Text;
                tt.HOADONBANLE = txt_Hoadonbanle.Text;

                qlxt_db.SubmitChanges();
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Vui lòng nhập thông tin chính xác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void txt_Solit_TextChanged(object sender, EventArgs e)
        {
            txt_Tongtien.Text = (double.Parse(txt_Dongia.Text) * double.Parse(txt_Solit.Text)).ToString();
        }

        private void txt_Dongia_TextChanged(object sender, EventArgs e)
        {
            txt_Tongtien.Text = (double.Parse(txt_Dongia.Text) * double.Parse(txt_Solit.Text)).ToString();
        }

        private void bnt_Thoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
